package com.itheima.health.controller.user;

import com.aliyun.oss.ClientException;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.SmsLoginParam;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.Result;
import com.itheima.health.service.MobileLoginService;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/mobile")
public class MobileLoginController {
    @Autowired
    private MobileLoginService mobileLoginService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealService setmealService;


    /**
     * 发送验证码
     *
     * @param telephone
     * @return
     * @throws ClientException
     */
    /**
     * 发送短信
     */
    @PostMapping("/validateCode/send")
    public Result send(String type, String telephone) throws ClientException {
        mobileLoginService.send(type, telephone);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 短信登录
     *
     * @param smsLoginParam
     * @return
     */
    @PostMapping("/login/smsLogin")
    public Result smsLogin(@RequestBody SmsLoginParam smsLoginParam) {
        String code = mobileLoginService.smsLogin(smsLoginParam); //redis中的验证码
        String validateCode = smsLoginParam.getValidateCode();//前端传递过来的验证码
        if (!code.equals(validateCode)) {
            return new Result(false, MessageConst.LOGIN_FAIL);
        }
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
    /**
     * 体检套餐
     *
     * @return
     */
    @GetMapping("/setmeal/getSetmeal")
    public Result getSetmeal() {
        try {
            List<Setmeal> list = setmealService.findAll();
            return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_SETMEAL_LIST_FAIL);
        }
    }

    /**
     * 体检套餐 //根据id查询套餐信息
     *
     * @param id
     * @return
     */
    @GetMapping("/setmeal/findById")
    public Result findById(@RequestParam Integer id) {
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, setmeal);
    }

}